package com.yang.mypc.sliingmenu;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Scroller;

/**
 * Created by MyPC on 2016/5/12.
 */
public class FlipperView extends ViewGroup {
    //menu
    private View menu;
    private View main;
    //
    private Scroller scroller;
    //flag 判断menu是否
    private boolean menuVisible = false ;
    //完全显示菜单需要移动的距离
    private int distance;

    public FlipperView(Context context, AttributeSet attrs) {

        super(context, attrs);
        scroller = new Scroller(context);
    }

    public FlipperView(Context context) {
        super(context);
    }

    public FlipperView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        distance = getWidth()*4/5;
        //底层菜单;
        menu  = getChildAt(0);
        //可见
        menu.setVisibility(VISIBLE);
        menu.measure(
                MeasureSpec.makeMeasureSpec(distance, MeasureSpec.EXACTLY),
                MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.EXACTLY));
        //位置
        menu.layout(-distance,0,0,getHeight());
        //主布局
        main = getChildAt(1);
        main.setVisibility(VISIBLE);
        main.measure(
                MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.EXACTLY),
                MeasureSpec.makeMeasureSpec(getHeight(), MeasureSpec.EXACTLY));
        main.layout(0,0,getWidth(),getHeight());

    }
    //展示方法  //设置滚动的方法
    public void showMenu(){
        if (!menuVisible) {
            scroller.startScroll(getScrollX(), 0, -distance, 0, 300);
            invalidate();
            menuVisible = true;
        }
    }
    @Override
    public void computeScroll() {
        // TODO Auto-generated method stub
        // 当滚动没有完成
        if (scroller.computeScrollOffset()) {
            scrollTo(scroller.getCurrX(), 0);
            postInvalidate();
        }
    }
    //隐藏方法
    public void hideMenu(){
        if (menuVisible) {
            scroller.startScroll(getScrollX(), 0, distance, 0, 300);
            invalidate();
            menuVisible = false;
        }
    }
}
